from src.subworld import *

class World():
    def __init__(self):
        self.subworld = []#这个是一个字符串的列表
        self.guy_num = 0
        self.guy_list = []#class
        self.guy_busy_list = []
        self.busy = 0
        print('世界生成完毕...')

    def add_guy(self):
        tar = Guy(self.guy_num)
        self.guy_num += 1
        self.guy_list.append(tar)
        self.guy_busy_list.append(tar.busy)

    def remove_guy(self, id = 0):
        for i in range(len(self.guy_list)):
            if i > id:
                self.guy_list[i] -= 1
        self.guy_list.pop(id)
        self.guy_busy_list.pop(id)

    def timemoves(self, time = 100):
        cnt = 0
        while cnt < time:
            m = self.timemove()
            cnt += m

    def timemove(self):
        a = min(self.guy_busy_list)#人物Busy的最小值
        #TODO 队伍busy的最小值。
        b = 1000
        #TODO 子世界busy的最小值。
        c = 1000
        #TODO 世界busy的最小值
        d = self.busy
        m = min([a,b,c,d])#总busy的最小值。
        s = [a,b,c,d].index(m)
        #TODO 扣除所有单位的busy
        self.change_all_busy(-m)
        #TODO Busy为零的单位行动
        if s == 0:
            id = self.guy_busy_list.index(0)
            #print(id)
            new_busy = self.guy_list[id].action()
            self.guy_busy_list[id] = new_busy
        elif s == 3:#
            print('世界发生了一些变动。')
            self.action()
        else:
            print("error!!!")
        return m

    def change_all_busy(self, busy_change = -1):
        #人物busy
        self.change_all_guy_busy(busy_change)
        #TODO 队伍busy
        #TODO 子世界busy
        #世界busy
        self.busy += busy_change

    def change_all_guy_busy(self, busy_change = -1):
        for guy_id in range(len(self.guy_list)):
            self.change_guy_busy(guy_id, busy_change)

    def change_guy_busy(self, guy_id, busy_change = -1):
        if self.guy_list[guy_id].disable == 0:
            self.guy_list[guy_id].busy += busy_change
            self.guy_busy_list[guy_id] += busy_change

    def reload_guy_busy_list(self):
        self.guy_busy_list = []
        for i in range(self.guy_num):
            self.guy_busy_list.append(self.guy_list[i].busy)

    #keyin
    def check_guy(self):
        length = len(self.guy_list)
        tmp = ''
        for i in range(length):
            tmp += f'【{i}】 {self.guy_list[i].name}  '
            if i%8 == 7:
                tmp += '\n'
        print(tmp)
        id = int(input("输入编号："))
        print(self.guy_list[id].history)

    def save(self):
        gl = self.guy_list
        with open(path+'/save/text.txt', 'wb') as file:
            pickle.dump(gl, file)

    def load(self):
        with open(path+'/save/text.txt', 'rb') as file:
            gl = pickle.load(file)
        self.guy_list = gl
        self.reload_guy_busy_list()

    def test(self):
        tmp = self.guy_list[0].__dir__()
        print(tmp)
        for i in range(len(tmp)):
            if '__module__' in tmp:
                print(tmp.pop())
            else:
                break
        print(tmp)

    #
    def action(self):
        if random.randint(0,10) < 6:
            self.add_guy()
        self.busy += 300 + 20 * random.randint(0, 10)
